package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.asserts.ParamAssertUtils;
import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.domain.dto.TenantEnteringDTO;
import cn.itsource.hrm.exception.ErrorEnum;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.hrm.util.encrypt.MD5;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-12-12
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void entering(TenantEnteringDTO enteringDTO) {

        Tenant tenant = enteringDTO.getTenant();
        Employee employee = enteringDTO.getEmployee();
        Long mealId = enteringDTO.getMealId();

        // 2.参数的合法性 非空校验
       // paramValid(tenant, employee);

        // 3.保存机构信息
        saveTanent(tenant);
        // 4.保存员工数据
        saveEmployee(tenant, employee);
        // 5.保存套餐;
        Date date = DateUtils.addDays(new Date(), 7);
        baseMapper.saveRelationMeal(mealId,tenant.getId(),date);

    }

    public void saveEmployee(Tenant tenant, Employee employee) {
        //密码的加盐加密
        String slat = MD5.getRandomCode(8);
        employee.setPassword(MD5.getMD5(employee.getPassword()+slat));
        employee.setSalt(slat);
        //注册时间
        employee.setInputTime(new Date());
        // 状态
        employee.setState(Employee.EMPLOYEE_STATE_NOMAL);
        // 关联机构
        employee.setTenantId(tenant.getId());
        // 员工类型；
        employee.setState(Employee.EMPLOYEE_TYPE_ADMIN_TENANT);
        employeeMapper.insert(employee);
    }

    private void saveTanent(Tenant tenant) {
        tenant.setRegisterTime(new Date());
        tenant.setState(Tenant.PENDING); //刚注册待审核的状态;
        baseMapper.insert(tenant);
    }

    private void paramValid(Tenant tenant, Employee employee) {
        ParamAssertUtils.assertNotEmpty(tenant.getCompanyName(), ErrorEnum.NOT_EMPTY.setMsg("公司名"));
        ParamAssertUtils.assertNotEmpty(tenant.getTenantTypeId(), ErrorEnum.NOT_EMPTY.setMsg("机构类型"));
        ParamAssertUtils.assertNotEmpty(tenant.getCompanyNum(),ErrorEnum.NOT_EMPTY.setMsg("营业执照"));
        ParamAssertUtils.assertNotEmpty(tenant.getAddress(),ErrorEnum.NOT_EMPTY.setMsg("公司地址"));
        ParamAssertUtils.assertNotEmpty(employee.getUsername(),ErrorEnum.NOT_EMPTY.setMsg("公司账号"));
        ParamAssertUtils.assertNotEmpty(employee.getPassword(),ErrorEnum.NOT_EMPTY.setMsg("密码"));
        ParamAssertUtils.assertIsEmail(employee.getEmail());
        ParamAssertUtils.assertIsPhone(employee.getTel());

        //验证参数是否重复
        Tenant tenantold = baseMapper.selectTenantByName(tenant.getCompanyName());
        ParamAssertUtils.assertISExist(tenantold,ErrorEnum.IS_EXIST.setMsg("公司名已存在"));
    }

}
